//
//  RSWRESTService+WorkSummary.m
//  RSW.IOS
//
//  Created by zhaiqiuqiu on 16/12/22.
//  Copyright © 2016年 com.xtion.crm. All rights reserved.
//

#import "RSWRESTService+WorkSummary.h"

@implementation RSWRESTService (WorkSummary)

-(XLGResult*)getMyWorkSummaryFilter:(NSString*)filter token:(NSString*)token  pageIndex:(NSInteger)pageIndex pageSize:(NSInteger)pageSize userNumber:(NSInteger)userNumber
{
    NSString *URI = [NSString stringWithFormat:@"api/worksummary?filter=%@&size=%ld&page=%ld&token=%@&userid=%ld", filter, (long)pageSize, (long)pageIndex, token, (long)userNumber];
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:nil HTTPMethod:XLGHTTPMethodTypeGet];
    
    if(r.outResult == YES)
    {
        NSArray *list = r.outObj[@"Value"];
        
        XMSQueueDictionary *qdSet = [WorkSummaryList JSONObjectProperty:nil];
        
        list = [CTransformHTTPRESTObj JSONObjs:list transformToProtocolObj:[WorkSummaryList class] accordingToColPropertyPair:qdSet];
        
        r = [XLGResult resultWithOutResult:YES outObj:list];
    }
    return r;
}

-(XLGResult*)getLeaderWorkSummaryFilter:(NSString*)filter token:(NSString*)token  pageIndex:(NSInteger)pageIndex pageSize:(NSInteger)pageSize
{
    NSString *URI = [NSString stringWithFormat:@"api/worksummary?filter=%@&size=%ld&page=%ld&token=%@", filter, (long)pageSize, (long)pageIndex, token];
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:nil HTTPMethod:XLGHTTPMethodTypeGet];
    
    if(r.outResult == YES)
    {
        NSArray *list = r.outObj[@"Value"];
        
        XMSQueueDictionary *qdSet = [WorkSummaryList JSONObjectProperty:nil];
        
        list = [CTransformHTTPRESTObj JSONObjs:list transformToProtocolObj:[WorkSummaryList class] accordingToColPropertyPair:qdSet];
        
        r = [XLGResult resultWithOutResult:YES outObj:list];
    }
    return r;
}

-(XLGResult*)addWorkSummary:(WorkSummaryInfo *)info token:(NSString*)token
{
    NSString *URI = [NSString stringWithFormat:@"api/worksummary?content=%@&token=%@&title=%@",info.content,token,info.title];
    
    NSDictionary *dic = @{
                          @"content":info.content,
                          @"token":token,
                          @"title":info.title
                          };
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:dic HTTPMethod:XLGHTTPMethodTypePost];
    
    if(r.outResult == YES && r.outObj != nil)
    {
        NSInteger success = [r.outObj[@"Code"] integerValue];
        
        if(success == YES)
        {
            return [XLGResult resultWithOutResult:r.outResult outObj:@(YES)];
        }
        else
        {
            NSError *error = r.outError;
            if(error == nil)
                error = [NSError errorWithDomain:r.outObj code:0 userInfo:nil];
            
            return [XLGResult resultWithOutResult:r.outResult outObj:@(NO) outError:error];
        }
    }
    
    return r;
}

-(XLGResult*)getWorkSummaryCommentById:(NSString*)workSummaryId token:(NSString*)token  pageIndex:(NSInteger)pageIndex pageSize:(NSInteger)pageSize
{
    NSString *URI = [NSString stringWithFormat:@"api/worksummarycomment?wid=%@&size=%ld&page=%ld&token=%@", workSummaryId, (long)pageSize, (long)pageIndex, token];
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:nil HTTPMethod:XLGHTTPMethodTypeGet];
    
    if(r.outResult == YES)
    {
        NSArray *list = r.outObj[@"Value"];
        
        XMSQueueDictionary *qdSet = [WorkSummaryCommentInfo JSONObjectProperty:nil];
        
        list = [CTransformHTTPRESTObj JSONObjs:list transformToProtocolObj:[WorkSummaryCommentInfo class] accordingToColPropertyPair:qdSet];
        
        r = [XLGResult resultWithOutResult:YES outObj:list];
    }
    return r;
}

-(XLGResult*)modifyWorkSummary:(WorkSummaryInfo *)info token:(NSString*)token
{
    NSString *URI = [NSString stringWithFormat:@"api/worksummary?content=%@&token=%@&title=%@&id=%@",info.content,token,info.title,info.workSummaryId];
    
    NSDictionary *dic = @{
                          @"content":info.content,
                          @"token":token,
                          @"title":info.title,
                          @"id":info.workSummaryId
                          };
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:dic HTTPMethod:XLGHTTPMethodTypePut];
    
    if(r.outResult == YES && r.outObj != nil)
    {
        NSInteger success = [r.outObj[@"Code"] integerValue];
        
        if(success == YES)
        {
            return [XLGResult resultWithOutResult:r.outResult outObj:@(YES)];
        }
        else
        {
            NSError *error = r.outError;
            if(error == nil)
                error = [NSError errorWithDomain:r.outObj code:0 userInfo:nil];
            
            return [XLGResult resultWithOutResult:r.outResult outObj:@(NO) outError:error];
        }
    }
    
    return r;
}

-(XLGResult*)deleteWorkSummary:(WorkSummaryInfo *)info token:(NSString*)token
{
    NSString *URI = [NSString stringWithFormat:@"api/worksummary?id=%@&token=%@",info.workSummaryId,token];
    
    NSDictionary *dic = @{
                          @"token":token,
                          @"id":info.workSummaryId
                          };
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:dic HTTPMethod:XLGHTTPMethodTypeDelete];
    
    if(r.outResult == YES && r.outObj != nil)
    {
        NSInteger success = [r.outObj[@"Code"] integerValue];
        
        if(success == YES)
        {
            return [XLGResult resultWithOutResult:r.outResult outObj:@(YES)];
        }
        else
        {
            NSError *error = r.outError;
            if(error == nil)
                error = [NSError errorWithDomain:r.outObj code:0 userInfo:nil];
            
            return [XLGResult resultWithOutResult:r.outResult outObj:@(NO) outError:error];
        }
    }
    
    return r;
}

-(XLGResult*)viewWorkSummary:(NSString *)workSummaryId token:(NSString*)token
{
    NSString *URI = [NSString stringWithFormat:@"api/worksummaryview?wid=%@&token=%@",workSummaryId,token];
    
    NSDictionary *dic = @{
                          @"wid":workSummaryId,
                          @"token":token
                          };
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:dic HTTPMethod:XLGHTTPMethodTypePost];
    
    if(r.outResult == YES && r.outObj != nil)
    {
        NSInteger success = [r.outObj[@"Code"] integerValue];
        
        if(success == YES)
        {
            return [XLGResult resultWithOutResult:r.outResult outObj:@(YES)];
        }
        else
        {
            NSError *error = r.outError;
            if(error == nil)
                error = [NSError errorWithDomain:r.outObj code:0 userInfo:nil];
            
            return [XLGResult resultWithOutResult:r.outResult outObj:@(NO) outError:error];
        }
    }
    
    return r;
}

-(XLGResult*)commentWorkSummary:(WorkSummaryInfo *)info token:(NSString*)token commentContent:(NSString*)commentContent
{
    NSString *URI = [NSString stringWithFormat:@"api/worksummarycomment?wid=%@&token=%@&content=%@",info.workSummaryId,token,commentContent];
    
    NSDictionary *dic = @{
                          @"wid":info.workSummaryId,
                          @"token":token,
                          @"content":commentContent
                          };
    
    XLGResult *r = [self HTTPRESTRequestURI:URI dicData:dic HTTPMethod:XLGHTTPMethodTypePost];
    
    if(r.outResult == YES && r.outObj != nil)
    {
        NSInteger success = [r.outObj[@"Code"] integerValue];
        
        if(success == YES)
        {
            return [XLGResult resultWithOutResult:r.outResult outObj:@(YES)];
        }
        else
        {
            NSError *error = r.outError;
            if(error == nil)
                error = [NSError errorWithDomain:r.outObj code:0 userInfo:nil];
            
            return [XLGResult resultWithOutResult:r.outResult outObj:@(NO) outError:error];
        }
    }
    
    return r;
}
@end
